<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回数组中的当前值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.count.html">« count</a></li>
      <li style="float: right;"><a href="function.each.html">each »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>返回数组中的当前值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.current" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">current</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">current</span> &mdash; <span class="dc-title">返回数组中的当前值</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.current-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>current</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">object</span></span> <code class="parameter">$array</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

    <p class="para rdfs-comment">
     每个数组中都有一个内部的指针指向它“当前的”单元，初始化时会指向该数组中的第一个值。
    </p>
   </div>

 <div class="refsect1 parameters" id="refsect1-function.current-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       要操作的数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.current-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>current()</strong></span>
   函数返回当前被内部指针指向的数组单元的值，并不移动指针。如果内部指针指向超出了单元列表的末端，<span class="function"><strong>current()</strong></span>
   将返回 <strong><code>false</code></strong>。
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code>false</code></strong>，但也可能返回等同于 <strong><code>false</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.current-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.1.0</td>
 <td>
  弃用在 <span class="type">object</span> 上调用此函数。
  在 <span class="type">object</span> 优先使用 <span class="function"><a href="function.get-mangled-object-vars.html" class="function">get_mangled_object_vars()</a></span> 或者使用 <span class="classname"><a href="class.arrayiterator.html" class="classname">ArrayIterator</a></span>。
 </td>
</tr>


    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.current-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4984">
    <p><strong>示例 #1  <span class="function"><strong>current()</strong></span> 函数使用示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$transport&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'foot'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bike'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'car'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'plane'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$transport</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$mode&nbsp;=&nbsp;'foot';<br /></span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">next</span><span style="color: #007700">(</span><span style="color: #0000BB">$transport</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$mode&nbsp;=&nbsp;'bike';<br /></span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$transport</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$mode&nbsp;=&nbsp;'bike';<br /></span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">prev</span><span style="color: #007700">(</span><span style="color: #0000BB">$transport</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$mode&nbsp;=&nbsp;'foot';<br /></span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">end</span><span style="color: #007700">(</span><span style="color: #0000BB">$transport</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$mode&nbsp;=&nbsp;'plane';<br /></span><span style="color: #0000BB">$mode&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$transport</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$mode&nbsp;=&nbsp;'plane';<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(false)<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(array());<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;array(0)&nbsp;{&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.current-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    在一个空数组上使用 <span class="function"><strong>current()</strong></span>
    函数，和在一个内部指针指向单元末端以外的数组上使用此函数，结果是相同的，同样都会返回
    <span class="type">bool</span> <strong><code>false</code></strong>。如果希望正确遍历一个包含 <strong><code>false</code></strong> 的数组，请参阅
    <a href="control-structures.foreach.html" class="link">foreach</a> 控制结构。
   </span>
   <span class="simpara">
    如果仍然希望使用 <span class="function"><strong>current()</strong></span> 来判断数组单元真实的值，应该同时使用
    <span class="function"><a href="function.key.html" class="function">key()</a></span> 和 <span class="function"><strong>current()</strong></span>
    来检查当前元素是否为 <strong><code>null</code></strong>。
   </span>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.current-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.end.html" class="function" rel="rdfs-seeAlso">end()</a> - 将数组的内部指针指向最后一个单元</span></li>
    <li class="member"><span class="function"><a href="function.key.html" class="function" rel="rdfs-seeAlso">key()</a> - 从关联数组中取得键名</span></li>
    <li class="member"><span class="function"><a href="function.each.html" class="function" rel="rdfs-seeAlso">each()</a> - 返回数组中当前的键／值对并将数组指针向前移动一步</span></li>
    <li class="member"><span class="function"><a href="function.prev.html" class="function" rel="rdfs-seeAlso">prev()</a> - 将数组的内部指针倒回一位</span></li>
    <li class="member"><span class="function"><a href="function.reset.html" class="function" rel="rdfs-seeAlso">reset()</a> - 将数组的内部指针指向第一个单元</span></li>
    <li class="member"><span class="function"><a href="function.next.html" class="function" rel="rdfs-seeAlso">next()</a> - 将数组中的内部指针向前移动一位</span></li>
    <li class="member"><a href="control-structures.foreach.html" class="link">foreach</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>